package com.woniu.cinema.controller;

import com.woniu.cinema.domain.Movie;
import com.woniu.cinema.dto.DynamicRequestMovieDto;
import com.woniu.cinema.service.MovieService;
import com.woniu.common.constant.UserConstants;
import com.woniu.common.core.controller.BaseController;
import com.woniu.common.core.domain.AjaxResult;
import com.woniu.common.core.page.TableDataInfo;
import com.woniu.common.core.page.TableSupport;
import com.woniu.common.core.redis.RedisCache;
import com.woniu.common.utils.ServletUtils;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author 杨光
 * @date 2022年04月02日 11:40
 */
@RestController
@RequestMapping("web/cinema/movie")
public class WebMovieController  extends BaseController {


    @Autowired
    private MovieService movieService;

    @Autowired
    private RedisCache redisCache;


    @Autowired
    private RedissonClient redissonClient;

    @GetMapping("/queryMoviesByCinemaId")
    public AjaxResult queryMoviesByCinemaId(Integer cinemaId) {
        List<Movie> list = movieService.queryMoviesByCinemaId(cinemaId);
        return AjaxResult.success(list);
    }


    @GetMapping(value = "queryMovieByMovieId")
    public AjaxResult getInfo(Integer movieId)
    {
        return AjaxResult.success(movieService.selectMovieVoById(movieId));
    }

    /**
     * 多条件动态查询
     * @author jitwxs
     * @date 2022/4/6 0006 10:10
     * @return null
     */

    @GetMapping("/dynamic/list")
    public TableDataInfo dynamiclist(DynamicRequestMovieDto movieDto) {
        startPage();
        List<Movie> list = movieService.dynamiclist(movieDto);
        return getDataTable(list);
    }

    /**
     * 查询影片信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Movie movie) {
        Integer pageNum = ServletUtils.getParameterToInt(TableSupport.PAGE_NUM);
        Integer pageSize = ServletUtils.getParameterToInt(TableSupport.PAGE_SIZE);
        String orderByColumn = ServletUtils.getParameter(TableSupport.ORDER_BY_COLUMN);
        String isAsc = ServletUtils.getParameter(TableSupport.IS_ASC);
        Integer movieStatus = movie.getMovieStatus();
        //缓存key
        String redisKey = UserConstants.REDIS_MOVIE_INDEX_PREFIX+pageNum+pageSize+orderByColumn+isAsc+movieStatus;
        //1.从redis中查询，查完直接返回
        List<Movie> cachedMovieList = redisCache.getCacheList(redisKey);
        if(!CollectionUtils.isEmpty(cachedMovieList)){
            return getDataTable(cachedMovieList);
        }
        //加上一把锁,使用setNX设置了超时时间5秒 ，难道就没问题了吗？
        //Redisson 加锁后，watchDog看门狗，开启一个单独的线程，每10秒监视代码是否执行完毕
        //如果没有执行完毕，那么把锁的过期时间再次设置为30秒
        //Redisson特点：
        //a.有看门狗的机制，能自动把锁续期
        //b.需要手动加锁，手动解锁
        RLock lock = redissonClient.getLock("redis_moive_index_cache_lock");
        List<Movie> list = null;
        try {
            lock.lock();
            //2.获得锁之后，再次查询redis，是否有结果
            List<Movie> cachedMovieList2 = redisCache.getCacheList(redisKey);
            if(!CollectionUtils.isEmpty(cachedMovieList2)){
                return getDataTable(cachedMovieList2);
            }
            //3.如果redis中没有，那么直接查询数据库
            startPage();
            list = movieService.selectMovieList(movie);
            //4.把数据库查询的结果放入redis
            redisCache.setCacheList(redisKey, list);
            System.out.println("缓存中没有查到，查询了数据库。。。。");
        }finally{
            //解除锁
            lock.unlock();
        }
        return getDataTable(list);
    }
}
